package stark.app.function.idiom;

import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.idiom.lib.xx.IdiomXxCharCell;
import com.stark.idiom.lib.xx.IdiomXxProvider;
import com.stark.idiom.lib.xx.view.IdiomXxView;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import stark.app.R;
import stark.app.databinding.ActivityIdiomXxBinding;
import stark.common.basic.anim.ViewFlyAnimator;
import stark.common.basic.base.BaseNoModelActivity;

/**
 * Author：daizhongbin on 2022/7/11
 * Email：1027154338@qq.com
 * Desc: 成语滑动消除游戏界面
 */
public class IdiomXxActivity extends BaseNoModelActivity<ActivityIdiomXxBinding> {

    private IdiomXxAdapter idiomAdapter;
    private SPUtils mSpUtils = SPUtils.getInstance();
    private static final String key_pass = "key_pass";
    private int mCurPass;

    @Override
    protected int onCreate() {
        return R.layout.activity_idiom_xx;
    }

    @Override
    protected void initView() {
        //获取关卡
        mCurPass = mSpUtils.getInt(key_pass, 1);
        List<IdiomXxCharCell> cellList = IdiomXxProvider.getCellList(mCurPass);
        mDataBinding.idiomView.setData(cellList);
        mDataBinding.idiomView.setListener(new IdiomXxView.IListener() {
            @Override
            public void onSelIdiom(@NonNull List<View> selViewList, String idiom, boolean isRight, boolean isConfirm) {
                mDataBinding.tvIdiom.setText(idiom);
                if (isConfirm) {
                    if (isRight) {
                        startAnimation(selViewList, idiomAdapter.getNextEmptyIdiomViewList(), idiom);
                    } else {
                        mDataBinding.tvIdiom.setText("");
                    }
                }
            }
        });

        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        mDataBinding.rvIdiom.setLayoutManager(layoutManager);
        IdiomXxAdapter idiomAdapter = new IdiomXxAdapter();
        this.idiomAdapter = idiomAdapter;
        idiomAdapter.initData(cellList.size() / 4);
        mDataBinding.rvIdiom.setAdapter(idiomAdapter);

        //提示按钮
        mDataBinding.btnPrompt.setOnClickListener(v -> {
            mDataBinding.idiomView.prompt();
        });
    }

    @Override
    protected void initData() {
    }

    private void startAnimation(List<View> fromViewList, List<View> toViewList, String idiom) {
        if (fromViewList == null || toViewList == null || fromViewList.size() != toViewList.size()) {
            return;
        }
        ViewFlyAnimator viewFlyAnimator = new ViewFlyAnimator(this);
        viewFlyAnimator.setCanTouch(false);
        long duration = mDataBinding.idiomView.getCellAnimDuration();
        viewFlyAnimator.start(fromViewList, toViewList, duration, null, new ViewFlyAnimator.IListener() {
            @Override
            public void onAnimEnd() {
                idiomAdapter.addIdiom(idiom);
                mDataBinding.idiomView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (mDataBinding.idiomView.checkComplete()) {
                            //完成了，则切换到下一关
                            ToastUtils.showShort("恭喜您完成了本关");
                            mDataBinding.idiomView.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    next();
                                }
                            }, 1000);
                        }
                    }
                }, 100);
            }
        });
    }

    private void next() {
        mCurPass++;
        int count = IdiomXxProvider.getLevelCount();
        if (mCurPass > count) {
            mCurPass = 1;
        }
        mSpUtils.put(key_pass, mCurPass);
        List<IdiomXxCharCell> cellList = IdiomXxProvider.getCellList(mCurPass);
        mDataBinding.idiomView.setData(cellList);
        idiomAdapter.initData(cellList.size() / 4);
    }
}
